/******************************** ************************ *********************************/
/********************************    JSON 字符串工具类     *********************************/
/******************************** ************************ *********************************/

import React, {Component,} from 'react';
import {AsyncStorage,} from "react-native"
import PropTypes from 'prop-types'; // ES6

class StorageUtil extends Component {
    /*
     * 保存数据
     * */
    static _saveData(key, value, isToast: PropTypes.bool) {
        if (isToast == null) { // == 和 === 的区别
            isToast = true;
        }
        try {
            AsyncStorage.setItem(String(key), String(value))
                .then((data) => {
                    if (data === null && isToast) {
                        ShowToast('存储成功');
                    }
                })
                .catch((error) => {
                    ShowToast(error)
                });
        } catch (error) {
            // Error saving data
            if (isToast) {
                ShowToast('There is a problem in saving message!');
            }
        }
    }

    /*
    * 读取数据
    * 返回一个Promise
    * */
    static _readData = async (key) => {
        try {
            const value = await AsyncStorage.getItem(String(key));
            if (value !== null) {
                return value;
            } else {
                return 'empty data!';
            }
        } catch (error) {
            return 'There is a problem in reading message!'
        }
    };

    /*
    * 删除某一数据
    * */
    static _deleteData(key, isToast: PropTypes.bool) {
        if (isToast == null) { // == 和 === 的区别
            isToast = true;
        }
        try {
            AsyncStorage.removeItem(String(key))
                .then((data) => {
                    if (isToast) {
                        if (data === null) {
                            ShowToast('删除成功');
                        } else {
                            ShowToast('' + data);
                        }
                    }
                })
                .catch((error) => {
                    ShowToast(error)
                });
        } catch (error) {
            if (isToast) {
                ShowToast('There is a problem in saving message!');
            }
        }
    }

    /*
    * 删除所有数据
    * */
    static _deleteAllData(isToast: PropTypes.bool) {
        if (isToast == null) { // == 和 === 的区别
            isToast = true;
        }
        try {
            AsyncStorage.clear()
                .then((data) => {
                    if (isToast) {
                        if (data === null) {
                            ShowToast('删除成功');
                        } else {
                            ShowToast('' + data);
                        }
                    }
                })
                .catch((error) => {
                    ShowToast(error)
                });
        } catch (error) {
            if (isToast) {
                ShowToast('There is a problem in saving message!');
            }
        }
    }

    /*
    * 获取所有本应用可以访问到的数据，不论来自什么库或调用者。返回一个Promise对象。
    * */
    static _getAllKey = async () => {
        try {
            const value = await AsyncStorage.getAllKeys();
            if (value !== null) {
                return value;
            } else {
                return 'empty data!';
            }
        } catch (error) {
            return 'There is a problem in reading message!'
        }
    }

    /*
    * 获取 keys 所包含的所有字段的值
    * */
    static _multiGet = async (keyArrays) => {
        try {
            const value = await AsyncStorage.multiGet(keyArrays);
            if (value !== null) {
                return value;
            } else {
                return 'empty data!';
            }
        } catch (error) {
            return 'There is a problem in reading message!'
        }
    }

}

export default StorageUtil;